package com.ggqq.day317;
//https://leetcode-cn.com/problems/majority-element/submissions/
import java.util.HashMap;
import java.util.Map;

public class Majority {

    public static void main(String[] args) {
        int[] matrix ={3,2,3};
        Majority majority = new Majority();
        System.out.println(majority.majorityElement(matrix));
    }

    public int majorityElement(int[] nums) {
        int n = nums.length/2;
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i=0;i<nums.length;i++){
            map.put(nums[i],map.getOrDefault(nums[i],0)+1);
        }
        for(int num:nums){
            if(map.get(num)>n){
                return num; //num是数组里的数字 不是索引！！
            }
        }
        return -1;


    }
}
